from sqlalchemy.orm import sessionmaker

from api.models.api_models import VideoSynthesis


class VideoSynthesisDAO:
    def __init__(self, engine):
        self.engine = engine
        self.Session = sessionmaker(bind=self.engine, expire_on_commit=False)

    def create(self, ppt_file_id, video_id, speech_file_id, video_synthesis=None):
        session = self.Session()
        video_synthesis = VideoSynthesis(ppt_file_id=ppt_file_id, video_id=video_id, speech_file_id=speech_file_id, video_synthesis=video_synthesis)
        session.add(video_synthesis)
        session.commit()
        session.close()
        return video_synthesis

    def read(self, ppt_file_id, video_id):
        session = self.Session()
        video_synthesis = session.query(VideoSynthesis).filter_by(ppt_file_id=ppt_file_id, video_id=video_id).first()
        session.close()
        return video_synthesis

    def update(self, ppt_file_id, video_id, video_synthesis=None):
        session = self.Session()
        video_synthesis = session.query(VideoSynthesis).filter_by(ppt_file_id=ppt_file_id, video_id=video_id).first()
        if video_synthesis is not None:
            video_synthesis.video_synthesis = video_synthesis
        session.commit()
        session.close()

    def delete(self, ppt_file_id, video_id):
        session = self.Session()
        video_synthesis = session.query(VideoSynthesis).filter_by(ppt_file_id=ppt_file_id, video_id=video_id).first()
        session.delete(video_synthesis)
        session.commit()
        session.close()